package webservices;

import interfaces.ISlideManage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import webservices.helper.Helper;

public class SlideManageImpl implements ISlideManage {
	
	public String update(String action, String slideID, String slideName, String description, String link, String courseID, String userID){
		String returnMessage = "";
		Connection con = null;
		try {
			con = Helper.getDBConnection();
			
			String log = "";
			
			if ("add".equals(action)){
				PreparedStatement pst = con.prepareStatement("insert into slide(name, description, link, courseid) values(?, ?, ?, ?)");
				pst.setString(1, slideName);
				pst.setString(2, description);
				pst.setString(3, link);
				pst.setString(4, courseID);
				pst.executeUpdate();
				log = "添加课件：";
			}  else if ("update".equals(action)){
				PreparedStatement pst = con.prepareStatement("update slide set name=?, description=?, link=? where slideid=?");
				pst.setString(1, slideName);
				pst.setString(2, description);
				pst.setString(3, link);
				pst.setString(4, slideID);
				pst.executeUpdate();
				log = "更新课件：";
			} else if ("delete".equals(action)){
				PreparedStatement pst = con.prepareStatement("delete from slide where slideid=?");
				pst.setString(1, slideID);
				pst.executeUpdate();
				log = "移除课件：";
			}
			
			log += slideName;
			
			PreparedStatement pst1 = con.prepareStatement("insert into changehistory(changetime, courseid, log, userid) values(now(), ?, ?, ?)");
			pst1.setString(1, courseID);
			pst1.setString(2, log);
			pst1.setString(3, userID);
			pst1.executeUpdate();
			
			returnMessage = "<response name='success' />";
		} catch (SQLException e) {
			e.printStackTrace();
			returnMessage = "<response name='fail' value ='课件操作失败：" + e.getLocalizedMessage() + "' />";
		} finally {
			try{
				con.close();
			}catch(SQLException e) {}
		}
		return returnMessage;
	}
	
}